// pages/cart/cart.ts
Page({
  data: {
    musics: [
      {
        id: 1,
        src:
          "http://218.205.239.34/MIGUM2.0/v1.0/content/sub/listenSong.do?netType=00&copyrightId=0&resourceType=2&contentId=600902000009525578&toneFlag=LQ&channel=02", // 歌曲资源
        title: "美人鱼 - 林俊杰", // 歌曲名称
        coverImgUrl:
          "http://d.musicapp.migu.cn/prod/file-service/file-down01/8121e8df41a5c12f48b69aea89b71dab/00f4563b7eaad0bd70d8f4e413e4d342/21214bce92c1774b72d2d7491b12395d", // 封面图
      },
      {
        id: 2,
        src:
          "http://218.205.239.34/MIGUM2.0/v1.0/content/sub/listenSong.do?netType=00&copyrightId=0&resourceType=2&contentId=600919000005413980&toneFlag=LQ&channel=02", // 歌曲资源
        title: "孤勇者 - 林俊杰", // 歌曲名称
        coverImgUrl:
          "http://d.musicapp.migu.cn/prod/file-service/file-down/8121e8df41a5c12f48b69aea89b71dab/ec2ab264dda5e4a87346f5002571ecef/aea3c247a293c47f67b018785a72a6b7", // 封面图
      },
    ],
    jishu:0, //第几首
    step: 0, //进度条
    currentTime: "00:00", //当前时间
    totalTime: "00:00", //结束事件


  },
  showtime:0,//总进度
  bgManager: {} as WechatMiniprogram.BackgroundAudioManager,
  onLoad(){
//设置某个tabar选项的小红点
wx.setTabBarBadge({
  index:2,//第几个
  text:'6',//个数
})
this.musice()
  },

  //请提示
  showToast(){
    wx.showToast({
      title:'hai~',
      mask:true,
      icon:'none'
    })
  },
  //加载中
  showloading(){
wx.showLoading({
  title:'加载中~'
})
setTimeout(()=>{
wx.hideLoading()
},1000)
  },
  //设置导航栏标题
  setNavTitle(){
    wx.setNavigationBarTitle({
      title:'我是导航',
    })
  },
  //选择图片
  chooseImage(){
    wx.chooseMedia({
      count:9,
      mediaType:['image','video'],//媒体选择类型
      sourceType:['album','camera'],
      maxDuration:30,
      camera:'back',
      success(res:any){
        console.log('图片信息',res);
      // let a=[...res.tempFiles] 
      //   console.log(a[0].size);
        
      }
    })
  },
  //创建音乐播放器
  musice(){
    //音乐播放器
    this.bgManager=wx.getBackgroundAudioManager()
    //监听音频播放事件,持续触发
    this.bgManager.onTimeUpdate(()=>{
      const settime=this.bgManager.currentTime;
      const gettime=this.bgManager.duration;
      // console.log(settime,gettime);
      console.log(parseInt(`${(settime/gettime)*100}`));
      this.setData({
        //进度条根据播放时长进行自动定位
        step:parseInt(`${(settime/gettime)*100}`),
        currentTime:this.formatTime(settime)
      })
      console.log(this.formatTime(settime));
    })
    //监听音频开始播放的事件,只执行一次
    this.bgManager.onPlay(()=>{})

    //监听歌曲播放结束事件
    this.bgManager.onEnded(()=>{
      if (this.data.jishu==this.data.musics.length-1) {
        //此时播放结束是最后一首
        this.setData({
          jishu:0,//重新播放
        })
      }else{
        this.setData({
          jishu:this.data.jishu+1
        })
      }
      this.setMusic() //调用函数进行播放
    })   
    //监听频频缓冲事件
    this.bgManager.onWaiting(()=>{
      wx.showToast({
        title:'网络较慢..凸(艹皿艹 )',
        icon:'none',
      })
    })
    //监听音频资源可以播放了,缓冲一定的数据时,可以播放了,不保证后面流畅播放
    this.bgManager.onCanplay(()=>{
      const curTime = this.bgManager.currentTime;
      const duration = this.bgManager.duration;
      this.showtime=duration
      this.setData({
        currentTime:this.formatTime(curTime),
        totalTime: this.formatTime(duration),
      })
      wx.hideToast()
      this.bgManager.play()
    })
    this.setMusic()
  },
  //时间转换
  formatTime(seconds:number):string{
    // 将总秒数，转化为 分:秒 的形式
    let sec: number | string = parseInt(`${seconds % 60}`);
    sec = sec < 10 ? "0" + sec : sec + "";
    let min: number | string = parseInt(`${seconds / 60}`);
    min = min < 10 ? "0" + min : min + "";
    return `${min}:${sec}`;
  },
  //选择音乐
  changeMusic(e:any){
this.setData({
  jishu:e.target.dataset.idx
})
this.setMusic()
  },
  //音乐播放
  setMusic(){
const curMusic=this.data.musics[this.data.jishu]
this.bgManager.src=curMusic.src
this.bgManager.title=curMusic.title
this.bgManager.coverImgUrl=curMusic.coverImgUrl
this.bgManager.play();
  },
  //进度条拉动音乐
  changeSlider(e: IEvent) {
    const curStep = e.detail.value;
    this.setData({
      step: curStep,
    });  
    this.bgManager.seek((curStep / 100) * this.showtime);
   
    
  },

})